package com.bluebench.bigul.domain.graphics.simple.curves;

import com.bluebench.bigul.domain.graphics.Graphic;
import com.bluebench.bigul.domain.graphics.Line;
import com.bluebench.bigul.domain.graphics.Point;
import com.bluebench.bigul.exception.GraphicInitializationException;
import com.itextpdf.text.pdf.PdfContentByte;

public class Circle extends Graphic{
	
	Line radius;
	
	public Circle(PdfContentByte canvas) {
		super(canvas);	
	}
	
	public Circle(Line radius){
		this.radius = radius;
	}
	
	@Override
	public Graphic build() throws GraphicInitializationException {
	   float centerX = vertices.get(0).getX();
	   float centerY = vertices.get(0).getY();
	   float r = this.radius.getLength();
	   float b=0.5523f;
	   canvas.moveTo(centerX + r, centerY);
       canvas.curveTo(centerX + r, centerY - r * b, centerX + r * b, centerY - r, centerX, centerY - r);
       canvas.curveTo(centerX - r * b, centerY - r, centerX - r, centerY - r * b, centerX - r, centerY);
       canvas.curveTo(centerX - r, centerY + r * b, centerX - r * b, centerY + r, centerX, centerY + r);
       canvas.curveTo(centerX + r * b, centerY + r, centerX + r, centerY + r * b, centerX + r, centerY);
       
       vertices.add(new Point(centerX,centerY));
       radius = new Line(r);
       
       return this;
	}
	
	
	public Graphic build(Point start, float r){
	   
       float centerX = start.getX();
	   float centerY = start.getY();
	   float b=0.5523f;
	   if(validate()){
		   canvas.moveTo(centerX + r, centerY);
		   canvas.curveTo(centerX + r, centerY - r * b, centerX + r * b, centerY - r, centerX, centerY - r);
		   canvas.curveTo(centerX - r * b, centerY - r, centerX - r, centerY - r * b, centerX - r, centerY);
		   canvas.curveTo(centerX - r, centerY + r * b, centerX - r * b, centerY + r, centerX, centerY + r);
		   canvas.curveTo(centerX + r * b, centerY + r, centerX + r, centerY + r * b, centerX + r, centerY);
	   }
	   
	   vertices.add(new Point(centerX,centerY));
       radius = new Line(r);
       
       return this;		
	}

	@Override
	protected boolean validate() {
		//TODO : Add Validation logic to ensure the circle does not fall out of the page or the margins
		return true;
	}

	@Override
	public Graphic concentric(float distance, boolean in) {
		float r = 0;
		if(in){
		  r = radius.getLength() - distance;		
		}else{
		  r = radius.getLength() + distance; 	
		}
		
		build(vertices.get(0),r);
		
		return this;
	}

	@Override
	public Graphic copy(float distance, int direction) {
		// TODO Auto-generated method stub
		return null;
	}

}
